package com.ym.medical.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.ym.common.enums.UserUserTypeEnum;

import com.ym.medical.domain.vo.IndexCountUserVo;
import com.ym.medical.domain.vo.OrderConsultantReportVo;
import com.ym.medical.domain.vo.UserReportVo;
import com.ym.medical.enums.InquiryOrderIoTypeEnum;
import com.ym.medical.service.ICommonSV;
import com.ym.medical.service.IReportCountSV;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * PC首页统计数据
 *
 * @author: 李涛
 * @version: 2019年07月01日 11:56
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ReportCountSVImpl implements IReportCountSV {

	@Autowired
	private ICommonSV commonSV;

	@Override
	public IndexCountUserVo getPersonnelStatistics(String startTime, String endTime) {
		IndexCountUserVo result = new IndexCountUserVo();
		Map<String, Object> params = new HashMap<>();
		params.put("startTime", startTime);
		params.put("endTime", endTime);
		// 医生患者数量查询
		String sql = " select count(0) userNum,user_type userType FROM user "
				+ " where create_time between #{params.startTime} and #{params.endTime}"
				+ " group by user_type";
		List<JSONObject> jsonObjects = commonSV.queryListJson(sql, params);
		for (JSONObject jsonObject : jsonObjects) {
			int userNum = jsonObject.getIntValue("userNum");
			String userType = jsonObject.getString("userType");
			if (UserUserTypeEnum.DOCTOR.getKey().equals(userType)) {
				result.setDoctorNum(userNum);
			} else if (UserUserTypeEnum.PATIENT.getKey().equals(userType)) {
				result.setPatientNum(userNum);
			}
		}
		// 查询咨询订单人次
		sql = "select count(0) orderNum,io_type ioType FROM inquiry_order "
				+ " where create_time between #{params.startTime} and #{params.endTime}"
				+ " group by io_type";
		jsonObjects = commonSV.queryListJson(sql, params);
		for (JSONObject jsonObject : jsonObjects) {
			int orderNum = jsonObject.getIntValue("orderNum");
			String ioType = jsonObject.getString("ioType");
			if (InquiryOrderIoTypeEnum.BY_CONSULTING.getKey().equals(ioType)) {
				result.setPictureNum(orderNum);
			} else if (InquiryOrderIoTypeEnum.TELEPHONE_COUNSELING.getKey().equals(ioType)) {
				result.setPhoneNum(orderNum);
			} else if (InquiryOrderIoTypeEnum.VIDEO_CONSULTATION.getKey().equals(ioType)) {
				result.setVideoNum(orderNum);
			}
		}
		return result;
	}

	@Override
	public List<OrderConsultantReportVo> getNumberOfConsultants(String startTime, String endTime, String ioType) {
		Map<String, Object> params = new HashMap<>();
		params.put("startTime", startTime);
		params.put("endTime", endTime);
		String sql = "select " +
				" count(0) num, " +
				" io_type ioType," +
				" DATE_FORMAT(create_time,'%Y-%m-%d') timeStr" +
				" from inquiry_order" +
				" where 1 = 1" +
				" and create_time between #{params.startTime} and #{params.endTime}";
		if (StringUtils.isNotBlank(ioType)) {
			params.put("ioType", ioType);
			sql += " and io_type = #{params.ioType}";
		}
		sql += "  group by ioType,timeStr order by timeStr";
		List<OrderConsultantReportVo> jsonObjects = commonSV.queryListEntity(sql, params, OrderConsultantReportVo.class);
		for (OrderConsultantReportVo jsonObject : jsonObjects) {
			jsonObject.setTime(DateUtil.parseDate(jsonObject.getTimeStr()).getTime());
		}
		return jsonObjects;
	}

	@Override
	public List<UserReportVo> getNumberOfRegisteredUser(String startTime, String endTime, String userType) {
		Map<String, Object> params = new HashMap<>();
		params.put("startTime", startTime);
		params.put("endTime", endTime);
		String sql = "select " +
				" count(0) num, " +
				" user_type userType,  " +
				"  DATE_FORMAT(create_time,'%Y-%m-%d') timeStr " +
				" from user where 1 = 1" +
				" and create_time between #{params.startTime} and #{params.endTime}";
		if (StringUtils.isNotBlank(userType)) {
			params.put("userType", userType);
			sql += " and user_type = #{params.ioType}";
		}
		sql += "  group by userType,timeStr order by timeStr ";
		List<UserReportVo> jsonObjects = commonSV.queryListEntity(sql, params, UserReportVo.class);
		for (UserReportVo jsonObject : jsonObjects) {
			jsonObject.setTime(DateUtil.parseDate(jsonObject.getTimeStr()).getTime());
		}
		return jsonObjects;
	}
}
